package com.bank.lenovoz5;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.text.TextUtils;

import com.entity.SMSCode;
import com.entity.User;
import com.receive.SMSReceiverSend;
import com.show.api.util.StringUtils;
import com.utils.ChatClient;
import com.utils.Constants;
import com.utils.FileHelper;
import com.utils.ImageView;
import com.utils.LogToFile;

import java.text.DecimalFormat;

import static android.os.SystemClock.sleep;
import static com.utils.ChatClient.C_updateBalance_W;
import static com.utils.ChatClient.ConfirmOrder;
import static com.utils.ChatClient.PAUSE;
import static com.utils.ChatClient.RUNING;
import static com.utils.ChatClient.S_paused_C;
import static com.utils.ChatClient.getSerialNumber;
import static com.utils.ChatClient.getmap;
import static com.utils.ChatClient.list;
import static com.utils.ChatClient.mapDelete;
import static com.utils.ChatClient.requestSMSCode;
import static com.utils.ChatClient.setDevicesLog;
import static com.utils.ChatClient.user;
import static com.utils.ChatClient.userInfo;
import static com.utils.Constants.orderBankName;
import static com.utils.UIUtils.showLog;

/**
 * 项目名称：app
 * 类描述：光大Z5脚本（不支持大额，不维护）
 * 创建人：Administrator
 * 创建时间：2019/11/6 14:06
 * 修改人：Administrator
 * 修改时间：2019/11/6 14:06
 * 修改备注：
 */
public class Z5CEBbank {

    private static int[][] xy_123 = new int[][]{
            {190, 1410}, {555, 1410}, {910, 1410},
            {190, 1580}, {555, 1580}, {910, 1580},
            {190, 1750}, {555, 1750}, {910, 1750},
            {555, 1930}
    };
    private static int[][] xy_123_abc = new int[][]{
            {60, 1280}, {160, 1280}, {260, 1280}, {390, 1280}, {486, 1280}, {600, 1280}, {700, 1280}, {810, 1280}, {920, 1280},
            {1020, 1280},
            {60, 1450}, {160, 1450}, {260, 1450}, {390, 1450}, {486, 1450}, {600, 1450}, {700, 1450}, {810, 1450}, {920, 1450},
            {1020, 1450},
            {100, 1615}, {210, 1615}, {320, 1615}, {430, 1615}, {540, 1615}, {650, 1615}, {760, 1615}, {870, 1615}, {980, 1615},
            {210, 1780}, {320, 1780}, {430, 1780}, {540, 1780}, {650, 1780}, {760, 1780}, {870, 1780}};
    private static int[][] sms_123 = new int[][]{
            {135, 1415}, {415, 1415}, {690, 1415},
            {135, 1580}, {415, 1580}, {690, 1580},
            {135, 1760}, {415, 1760}, {690, 1760},
            {135, 1930}, {415, 1930}
    };
    static SMSReceiverSend sMSReceiverSend;
    private static UiDevice mDevice;
    private static String type = "ceb";
    private static String bankName = "CEB";
    public static boolean login = false;
    private static String sumMoney = "-1";
    private static int screenHeight = 0; // 手机屏幕高度
    private static int screenWidth = 0; // 手机屏幕宽度
    private static Context context = InstrumentationRegistry.getTargetContext();
    private final static boolean SMS_test = false;
    private final String SMS_ERR = "sms_err";
    private final String SUCCESS = "success";

    public Z5CEBbank(UiDevice mDevice) throws UiObjectNotFoundException {
        this.mDevice = mDevice;
        initInfo();
    }

    private void initInfo() throws UiObjectNotFoundException {
        //动态注册短信广播
        Constants.MOBILE_BOB = "95595";
        Constants.CODE = "";
        sMSReceiverSend = new SMSReceiverSend();
        Constants.MOBILE_BOB_86 = "+8695595";
        IntentFilter filter = new IntentFilter();
        filter.addAction(Constants.SMS_ACTION);
        // 设置优先级 不然监听不到短信
        //        filter.addAction("net.blogjava.mobile.SMSto");
        filter.setPriority(1000);
        context.registerReceiver(sMSReceiverSend, filter);
        Intent intent = new Intent();
        intent.setAction("com.tiantian.SEND_TO");
        context.sendBroadcast(intent);
        //获取引导页面上更新弹窗并关闭
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtime_5) {
                break;
            }
            UiObject update = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile.cemb:id/dialog_tv_title"));
            UiObject mBT_dialog = mDevice.findObject(new UiSelector().resourceId("android:id/button2").text("是"));
            if (update.exists()) {
                try {
                    if (update.getText().contains("发现新版本")) {
                        UiObject no_update = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                                ".cemb:id/dialog_btn_negative"));
                        if (no_update.exists()) {
                            showMsg("======关闭跟新弹窗======");
                            no_update.click();
                            sleep(ChatClient.overtime_3);
                            break;
                        }
                    }
                } catch (UiObjectNotFoundException e) {
                    e.printStackTrace();
                }
            }
            if (mBT_dialog.exists()) {
                showMsg("======关闭风险提示弹窗======");
                mBT_dialog.click();
            }
        }

        //获取主页面上广告弹窗并关闭
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtime_15) {
                showMsg("======获取首页弹出广告超时======");
                break;
            }
            UiObject guanggao = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                    ".cemb:id/home_advertising_iv_close"));
            if (guanggao.exists()) {
                showMsg("关闭广告弹窗");
                guanggao.click();
                break;
            }
        }
        //获取页面上转账汇款节点
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtimeLogin) {
                ChatClient.timer(LogToFile.getMethodName(), ChatClient.errorStr);
                return;
            }
            UiObject zhuanzhang = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                    ".cemb:id/ll_imqq_transfer_remittance"));
            if (zhuanzhang.exists()) {
                showMsg("点击---转账汇款");
                zhuanzhang.click();
                sleep(1000);
                ImageView.saveImg("");
                //连接服务端
                ChatClient.start();
                //获取账号信息
                ChatClient.begintime = System.currentTimeMillis();//开始时间
                while (true) {
                    long nowtime1 = System.currentTimeMillis();
                    if ((nowtime1 - ChatClient.begintime) > ChatClient.overtime) {
                        ChatClient.timer(LogToFile.getMethodName(), ChatClient.errorStr);
                        return;
                    }
                    if (userInfo.equals("")) {
                        ChatClient.msg("<YZF>{'header':'" + ChatClient.M_GET_BANK_LOGIN_INFO_P + "'}</YZF>");
                        sleep(5000);
                    } else {
                        user = new User();
                        String[] res = userInfo.split(" ");
                        for (int i = 0; i < res.length; i++) {
                            switch (i) {
                                case 0:
                                    user.setUserName(res[0]);
                                    break;
                                case 1:
                                    user.setPwd(res[1]);
                                    break;
                                case 2:
                                    user.setBankpwd(res[2]);
                                    break;
                                case 3:
                                    user.setUsbPayPwd(res[3]);
                                    break;
                                case 4:
                                    user.setUseType(res[4]);
                                    break;
                            }
                        }
                        break;
                    }
                }
                break;
            }
        }
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            UiObject pwd = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile.cemb:id/login_et_pwd"));
            if (pwd.exists()) {
                showMsg("进入登录界面");
                break;
            }
        }
        Login();
    }

    //进入登录页面补充用户信息
    public void Login() throws UiObjectNotFoundException {
        inputBankPwd();
        sleep(ChatClient.overtime_5);
        while (true) {
            UiObject mTV_transfer = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                    ".cemb:id/tv_transfer_main_transfer").text("转账汇款"));
            if (mTV_transfer.exists()) {
                showMsg("=====登录成功========");
                break;
            }
            UiObject ok = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile.cemb:id/login_btn_login"));
            //判断银行密码
            if (ok.exists()) {
                showMsg("=====确定登录=====");
                ok.click();
                break;
            }
        }
        sleep(ChatClient.overtime_3);
        clickListPage();
        ChatClient.loginS();
        //是否从在订单
        setRemittance();
    }

    /**
     * 填充登陆密码
     *
     * @throws UiObjectNotFoundException 异常捕获
     */
    private void inputBankPwd() throws UiObjectNotFoundException {
        ChatClient.begintime = System.currentTimeMillis();
        while (true) {
            long nowTime = System.currentTimeMillis();
            sleep(1000);
            if (nowTime - ChatClient.begintime > ChatClient.overtime) {
                ChatClient.timer("Login", "登录超时3");
                return;
            }
            UiObject pwd = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile.cemb:id/login_et_pwd"));
            if (pwd.exists()) {
                showMsg("点击密码输入栏");
                pwd.click();
                sleep(ChatClient.overtime_3);
                setClick(user.getPwd());
                break;
            }
        }
        // 收起键盘
        showMsg("点击密码输入栏");
        mDevice.click(945, 1940);
    }

    /**
     * 登录成功后进入转账
     */
    public void setRemittance() {
        try {
            while (true) {
                sleep(ChatClient.overtime_10);
                RUNING = list.size() > 0;
                if (RUNING) {
                    orderBankName = Constants.getBank(list.get(0).getBankName(), "CEB");
                    if (ConfirmOrder) {
                        //查账
                        showMsg("====进入查账逻辑===");
                        auditOrder();
                    } else {
                        showMsg("====进入转账逻辑===");
                        remittanceLongin();
                    }
                } else {
                    if (PAUSE) {
                        showMsg("====等待订单====");
                        remittance1();
                    } else {
                        S_paused_C();
                        sleep(1000);
                    }
                }
            }
        } catch (Exception e) {
            setDevicesLog(e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 转账汇款
     */
    public void remittanceLongin() {
        try {
            clickListPage();
            // 获取金额
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtimewait) {
                    showMsg("余额获取超时");
                    break;
                }
                UiObject money = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                        ".cemb:id/tv_transfer_input_account_of_payment_balance"));
                if (money.exists()) {
                    showMsg("money:" + money.getText());
                    sumMoney = money.getText();
                    sumMoney = sumMoney.replaceAll(",", "");
                    break;
                }
            }

            if (sumMoney.equals("")) {
                sumMoney = "-1";
            } else {
                showMsg("本地保存余额");
                String sumMoneyFile = user.getUserName() + bankName + "_sumMoney.txt";
                FileHelper.saveMoneySDFile(false, sumMoney, sumMoneyFile);
            }
            showMsg("=====获取当前金额=====" + sumMoney);
            //账户余额是否大于转账金额
            if (Double.parseDouble(sumMoney) < Double.parseDouble(list.get(0).getMoney())) {
                showMsg("=====账户余额不足=====");
                String mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "','phoneModel':'光大银行'," +
                        "'info':'账户余额不足'";
                String json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':1,'type':1";
                ChatClient.msg("<YZF>{'header':'" + ChatClient.M_SEND_TRADE_RESULT_P + "'," + json + "," + mobileD + "}</YZF>");
                //删除处理过的订单
                mapDelete(list.get(0).getOrder());
                getmap();
                sleep(ChatClient.overtime_3);
                // 返回
                backMainPage();
                return;
            }

            if (!sumMoney.equals("")) {
                C_updateBalance_W(Double.parseDouble(sumMoney));
                sleep(1000);
            }


            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime) {
                    ChatClient.timer(LogToFile.getMethodName(), ChatClient.errorStr);
                    return;
                }
                //                UiObject username_ = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile
                // .cemb:id/et_transfer_name"));
                //                if (username_.exists()) {
                //                    username_.setText(list.get(0).getUserName());
                //                    LogToFile.d("====输入账户姓名====", "===输入账户姓名====");
                //                    break;
                //                }
                UiObject username_1 = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                        ".cemb:id/et_transfer_input_first_transfer_payee_name"));

                if (username_1.exists()) {
                    showMsg("====输入账户姓名====");
                    username_1.setText(list.get(0).getUserName());
                    break;
                }
            }
            sleep(ChatClient.overtime_3);
            // 收起收款人列表
            mDevice.click(130, 310);
            sleep(2000);
            //            UiObject bankNo_ = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile
            // .cemb:id/et_transfer_account"));
            //            if (bankNo_.exists()) {
            //                bankNo_.setText(list.get(0).getBankNo());
            //            }
            while (true) {
                UiObject bankNo_1 = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                        ".cemb:id/et_transfer_input_first_transfer_payee_account"));
                if (bankNo_1.exists()) {
                    showMsg("====输入卡号====");
                    bankNo_1.setText(list.get(0).getBankNo());
                    break;
                }
            }

            sleep(2000);
            while (true) {
                //                UiObject xzyh1 =mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile
                // .cemb:id/tv_transfer_bank"));
                //                if (xzyh1.exists()) {
                //                    xzyh1.click();
                //                    LogToFile.d("=====点击选择银行====", "=====点击选择银行====");
                //                    break;
                //                }
                UiObject xzyh2 = mDevice.findObject(new UiSelector().
                        resourceId("com.cebbank.mobile.cemb:id/tv_transfer_input_first_transfer_payee_bank"));
                if (xzyh2.exists()) {
                    showMsg("=====点击选择银行====");
                    xzyh2.click();
                    break;
                }
            }
            sleep(ChatClient.overtime_3);
            while (true) {
                UiObject bankName_search = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                        ".cemb:id/et_bankname_search"));
                //判断是否进入转账页面
                if (bankName_search.exists()) {
                    showMsg("=====进入选择银行页面====");
                    sleep(1000);
                    bankName_search.setText(orderBankName);
                    break;
                }
            }
            // 搜索
            while (true) {
                UiObject iv_bankname_find = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                        ".cemb:id/iv_bankname_find"));
                //判断是否进入转账页面
                if (iv_bankname_find.exists()) {
                    showMsg("====搜索银行名称====");
                    iv_bankname_find.click();
                    break;
                }
            }
            sleep(ChatClient.overtime_3);
            showMsg("=====收款银行====" + orderBankName);

            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime3 = System.currentTimeMillis();
                if ((nowtime3 - ChatClient.begintime) > ChatClient.overtime) {
                    ChatClient.timer(LogToFile.getMethodName(), ChatClient.errorStr);
                    return;
                }
                UiObject mLV = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile.cemb:id/chobank_lv_banks"));
                //判断是否进入转账页面
                if (mLV.exists()) {
                    UiObject mLL = mLV.getChild(new UiSelector().className("android.widget.LinearLayout").index(1));
                    if (mLL.exists()) {
                        UiObject mLL_item = mLL.getChild(new UiSelector().resourceId("com.cebbank.mobile.cemb:id/ll_bank_item"));
                        showMsg("=====收款银行，点击====" + orderBankName);
                        mLL_item.click();
                        break;
                    }
                }
            }
            //            }

            sleep(ChatClient.overtime_2);
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > 60000) {
                    ChatClient.timer(LogToFile.getMethodName(), ChatClient.errorStr);
                    break;
                }
                //                UiObject next = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile
                // .cemb:id/btn_transfer_next"));
                //                if (next.exists()) {
                //                    break;
                //                }
                UiObject next2 = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                        ".cemb:id/tv_transfer_input_next")
                        .text("下一步"));

                //判断是否进入转账页面
                if (next2.exists()) {
                    showMsg("返回转账单页");
                    break;
                }
            }

            // 输入金额
            while (true) {
                //                UiObject jine = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile
                // .cemb:id/et_transfer_money"));
                //                if (jine.exists()) {
                //                    jine.setText(list.get(0).getMoney());
                //                    break;
                //                }
                UiObject jine2 = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                        ".cemb:id/et_transfer_input_transfer_number_of_money"));
                if (jine2.exists()) {
                    showMsg("输入金额");
                    jine2.setText(list.get(0).getMoney());
                    break;
                }
            }
            sleep(1000);

            //下一步
            showMsg("=====点击下一步=====");
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > 60000) {
                    ChatClient.timer(LogToFile.getMethodName(), ChatClient.errorStr);
                    break;
                }
                //                UiObject next = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile
                // .cemb:id/btn_transfer_next"));
                //                if (next.exists()) {
                //                    next.click();
                //                    break;
                //                }
                UiObject next2 = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                        ".cemb:id/tv_transfer_input_next")
                        .text("下一步"));
                if (next2.exists()) {
                    next2.click();
                    break;
                }
            }

            String error = checkSmsByCommitError();
            if (!StringUtils.isEmpty(error) && !SUCCESS.equals(error)) {
                showMsg("====账单失败原因：" + error + "====");
                String mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "','phoneModel':'光大银行'," +
                        "'info':'账户余额不足'";
                String json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':1,'type':1";
                ChatClient.msg("<YZF>{'header':'" + ChatClient.M_SEND_TRADE_RESULT_P + "'," + json + "," + mobileD + "}</YZF>");
                //删除处理过的订单
                mapDelete(list.get(0).getOrder());
                getmap();
                sleep(ChatClient.overtime_3);
                // 返回
                backMainPage();
                return;
            }
            //进入输入密码
            clickBankPwd();
            sleep(ChatClient.overtime_5);
            String msg = clickMsg();
            if (!msg.equals(SUCCESS)) {
                ChatClient.msg("<YZF>{'header':'" + ChatClient.M_TRANSFERFINISH_P + "'}</YZF>");
                sleep(1000);
                reportTransferOrderStatus(msg, Constants.STATE_FAIL);
                backMainPage();
                sleep(ChatClient.overtime_3);
                clickListPage();
                return;
            }
            checkFinish();
            showMsg("=====进入转账完成===");
            ChatClient.msg("<YZF>{'header':'" + ChatClient.M_TRANSFERFINISH_P + "'}</YZF>");

            //获取转账完成状态
            String balance = "";
            if (Constants.getStr(type) == 0) {
                DecimalFormat formater = new DecimalFormat("#0.##");
                balance = formater.format(Double.parseDouble(sumMoney) - Double.parseDouble(list.get(0).getMoney()));
            }
            sleep(2000);
            LogToFile.d("====---money-====", "money:" + balance);
            type = type.replaceAll("\r|\n|\\s", "");
            String mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "','phoneModel':'光大银行'," +
                    "'info':'" + type + "'";
            //            String json = "'order':'" + list.get(0).getOrder() + "','balance':'" + balance + "','state':" +
            // Constants.getStr(type) + "";
            String json;
            if (Constants.getStr(type) == 1) {//shi bai
                json = "'order':'" + list.get(0).getOrder() + "','balance':'" + balance + "','state':" + Constants.getStr(type)
                        + ",'type':1";
            } else {
                json = "'order':'" + list.get(0).getOrder() + "','balance':'" + balance + "','state':" + Constants.getStr(type)
                        + ",'type':1";
            }
            ChatClient.msg("<YZF>{'header':'" + ChatClient.M_SEND_TRADE_RESULT_P + "'," + json + "," + mobileD + "}</YZF>");
            //删除处理过的订单
            mapDelete(list.get(0).getOrder());
            getmap();
            sleep(5000);

            //返回
            while (true) {
                UiObject back0 = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                        ".cemb:id/ll_title_right_drawable_top_and_word_bottom_root"));
                UiObject back1 = mDevice.findObject(new UiSelector().
                        resourceId("com.cebbank.mobile.cemb:id/tv_transfer_result_continue_transfer"));
                if (back0.exists()) {
                    LogToFile.d("=====转账完成返回====", "=====转账完成返回===");
                    back0.click();
                    break;
                }
                if (back1.exists()) {
                    LogToFile.d("=====转账完成返回====", "=====转账完成返回===");
                    back1.click();
                    break;
                }
            }

            sleep(ChatClient.overtime_3);
            clickListPage();
        } catch (Exception e) {
            setDevicesLog(e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 输入交易密码
     *
     * @throws UiObjectNotFoundException 异常捕获
     */
    private void clickBankPwd() throws UiObjectNotFoundException {
        showMsg("=====进入输入密码=====");
        mDevice.click(540, 540);
        while (true) {
            UiObject pageTitle = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                    ".cemb:id/tv_transfer_certification_confirm_title_name").text("请输入交易密码"));
            if (pageTitle.exists()) {
                showMsg("进入密码输入页面");
                break;
            }
        }

        while (true) {
            UiObject mET_pwd = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                    ".cemb:id/et_transfer_certification_confirm_input_pwd_number"));
            if (mET_pwd.exists()) {
                showMsg("点击密码栏，从新打开键盘");
                mET_pwd.click();
                sleep(2000);
                showMsg("点击密码");
                setClick_bankPwd(user.getBankpwd());
                sleep(2000);
                break;
            }
        }
        showMsg("收起键盘");
        mDevice.click(540, 540);
        sleep(2000);

        while (true) {
            UiObject mBT_confirm = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                    ".cemb:id/tv_transfer_certification_confirm_transfer").text("确认转账"));
            if (mBT_confirm.exists()) {
                showMsg("点击---确认转账");
                mBT_confirm.click();
                break;
            }
        }
    }

    private void checkFinish() throws UiObjectNotFoundException {
        long startTime = System.currentTimeMillis();
        LogToFile.e("-checkFinish-", "开始检查是否到结果页");
        while (true) {
            long nowTime = System.currentTimeMillis();
            sleep(1000);
            if (nowTime - startTime > ChatClient.overtime) {
                ChatClient.timer("remittanceLongin", "转账超时");
                return;
            }
            UiObject info = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile.cemb:id/tv_success_trans_state"));
            UiObject info2 = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                    ".cemb:id/tv_transfer_result_success_state"));
            //            UiObject bankPwd1 = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile
            // .cemb:id/et_trader_password"));
            //            UiObject bankPwd2 = mDevice.findObject(new UiSelector().resourceId("android:id/content"));
            //            UiObject message = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
            //                    ".cemb:id/btn_get_identifying_code"));
            //            UiObject message2 = mDevice.findObject(new UiSelector().
            //                    resourceId("com.cebbank.mobile.cemb:id/tv_transfer_payment_view_obtain_code"));
            //            if (bankPwd1.exists() || bankPwd2.exists()) {
            //                LogToFile.e("-checkFinish-", "检查到需要重新输入银行密码");
            //                clickBankPwd();
            //            }
            //            if (message.exists() || message2.exists()) {
            //                LogToFile.e("-checkFinish-", "检查到需要重新输入短信验证码");
            //                clickMsg();
            //            }
            if (info.exists()) {
                LogToFile.e("-checkFinish-", "检查完成1");
                type = info.getText();
                sleep(1000);
                break;
            }
            if (info2.exists()) {
                LogToFile.e("-checkFinish-", "检查完成2");
                type = info2.getText();
                sleep(1000);
                break;
            }
        }
        LogToFile.e("-checkFinish-", "检查完成，退出");
    }

    /**
     * 输入交易短信验证码
     *
     * @throws UiObjectNotFoundException 异常捕获
     */
    private String clickMsg() throws UiObjectNotFoundException {
        Constants.SMSMESSAGE = "";
        sleep(2000);
        //获取短信验证码
        showMsg("=====准备获取短信验证码2=====");
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtime) {
                ChatClient.timer("getSMSCode", ChatClient.errorStr);
                return "短信页出错";
            }
            UiObject message = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                    ".cemb:id/btn_get_identifying_code"));
            if (message.exists()) {
                showMsg("===点击获取短信验证码====");
                message.click();
                break;
            }
            UiObject message2 = mDevice.findObject(new UiSelector().
                    resourceId("com.cebbank.mobile.cemb:id/tv_transfer_payment_view_obtain_code"));
            if (message2.exists()) {
                showMsg("===点击获取短信验证码====");
                message2.click();
                break;
            }
        }
        sleep(ChatClient.overtime_10);
        String useType = user.getUseType();
        if (useType.equals("1")) {
            if (SMS_test) {
                checkSms();
            } else {
                getMsgByPhone();
                setClick_SMS(Constants.CODE);
                Constants.CODE = "";
            }
        } else {
            showMsg("处理转账短信---无SIM卡");
            setNotSIM_SMS();
        }
        sleep(2000);
        mDevice.click(900, 1930);
        sleep(2000);
        while (true) {
            UiObject ok = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile.cemb:id/btn_transfer_submit"));
            UiObject ok2 = mDevice.findObject(new UiSelector().
                    resourceId("com.cebbank.mobile.cemb:id/tv_transfer_certification_confirm_transfer").text("提交"));

            //判断银行密码
            if (ok.exists()) {
                showMsg("=====确定转账=====");
                ok.click();
                break;
            }
            if (ok2.exists()) {
                showMsg("=====确定转账=====");
                ok2.click();
                break;
            }
        }

        sleep(2000);
        String err = checkSmsByCommitError();
        if (checkSmsByCommitError().equals(SMS_ERR)) {
            return clickMsg();
        } else {
            return err;
        }
    }

    /**
     * 处理无SIM卡手机短信逻辑
     */
    private void setNotSIM_SMS() throws UiObjectNotFoundException {
        sleep(ChatClient.overtime_2);
        sleep(ChatClient.overtime_20);
        SMSCode smsCode = requestSMSCode(user.getUserName(), user.getBankId());
        if (StringUtils.isEmpty(smsCode.getSmscode())) {
            sleep(ChatClient.overtime_20);
            smsCode = requestSMSCode(user.getUserName(), user.getBankId());
        }
        showMsg("返回短信验证码为：" + smsCode.getSmscode());
        while (true) {
            UiObject kl = mDevice.findObject(new UiSelector().text("请输入短信验证码").resourceId("com.cebbank.mobile.cemb:id/tv_cuskey"));
            if (kl.exists()) {
                setClick_SMS(smsCode.getSmscode());
                break;
            }
        }
    }

    /**
     * 真机短信验证码
     */
    private void getMsgByPhone() {
        Exception exception = new Exception();
        StackTraceElement[] trace = exception.getStackTrace();
        String method = trace[0].getMethodName();
        //判断是否获取到短信
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtime) {
                ChatClient.timer(method, ChatClient.errorStr);
                return;
            }

            while (Constants.CODE.equals("")) {
                showMsg(">>>>>获取短信验证码");
                sleep(1000);
            }
            break;
        }
    }

    /**
     * 查账
     *
     * @throws UiObjectNotFoundException 异常捕获
     */
    public void auditOrder() throws UiObjectNotFoundException {
        while (true) {
            UiObject zhuanzhang1 = mDevice.findObject(new UiSelector().text("请输入收款户名/首字母检索").resourceId("com.cebbank.mobile" +
                    ".cemb:id/et_transfer_name"));
            UiObject zhuanzhang2 = mDevice.findObject(new UiSelector().text("请输入收款户名/首字母检索").
                    resourceId("com.cebbank.mobile.cemb:id/et_transfer_input_first_transfer_payee_name"));
            if (zhuanzhang1.exists()) {
                showMsg("账单页面");
                break;
            }
            if (zhuanzhang2.exists()) {
                showMsg("账单页面");
                break;
            }
        }
        //进入转账页面
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtimewait) {
                break;
            }
            UiObject money = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile.cemb:id/text_approve"));
            UiObject money2 = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                    ".cemb:id/tv_transfer_input_account_of_payment_balance"));
            if (money.exists()) {
                showMsg("textView1:" + money.getText());
                sumMoney = money.getText();
                sumMoney = sumMoney.replaceAll(",", "");
                showMsg("===moeny1====" + sumMoney + "===");
                if (!sumMoney.equals("")) {
                    C_updateBalance_W(Double.parseDouble(sumMoney));
                    sleep(1000);
                }
                break;
            }
            if (money2.exists()) {
                showMsg("textView2:" + money2.getText());
                sumMoney = money2.getText();
                sumMoney = sumMoney.replaceAll(",", "");
                showMsg("===moeny2====" + sumMoney + "===");
                if (!sumMoney.equals("")) {
                    C_updateBalance_W(Double.parseDouble(sumMoney));
                    sleep(1000);
                }
                break;
            }
        }
        sleep(1000);
        // 返回
        backMainPage();
        sleep(1000);
        while (true) {
            UiObject chazhang = mDevice.findObject(new UiSelector().className("android.widget.TextView").text("转账明细"));
            sleep(1000);
            showMsg("===查找《转账明细》===");
            if (chazhang.exists()) {
                showMsg("===找到《转账明细》===");
                chazhang.click();
                break;
            }
        }
        sleep(ChatClient.overtime_5);
        while (true) {
            UiObject chazhang1 = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile.cemb:id/tv_center_title").text
                    ("转账明细查询"));
            UiObject chazhang2 = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile.cemb:id/tv_center_title")
                    .descriptionContains("转账明细查询"));
            if (chazhang1.exists() || chazhang2.exists()) {
                showMsg("===《转账明细》页面加载完成===");
                break;
            }
        }
        //判断是否进入交易明细
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            sleep(1000);
            if ((nowtime - ChatClient.begintime) > ChatClient.overtime_60) {
                String json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':6,'type':1";
                String mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "', 'phoneModel':'光大银行', " +
                        "'info':'查询超时'";
                ChatClient.msg("<YZF>{'header':'" + ChatClient.S_CONFIRM_TRADE_RESULT_C + "'," + json + "," + mobileD +
                        "}</YZF>");
                //删除处理过的订单
                mapDelete(list.get(0).getOrder());
                getmap();
                // 返回
                backMainPage();
                sleep(ChatClient.overtime_3);
                clickListPage();
                return;
            }
            try {
                long startTime = System.currentTimeMillis();
                while (true) {
                    long nowTime = System.currentTimeMillis();
                    sleep(1000);
                    if (nowTime - startTime > ChatClient.overtime) {
                        ChatClient.timer("auditOrder", "获取查账页元素超时");
                        return;
                    }
                    /*
                     *判断是否没有转账记录
                     */
                    boolean notList;
                    startTime = System.currentTimeMillis();
                    while (true) {
                        nowTime = System.currentTimeMillis();
                        if (nowTime - startTime > ChatClient.overtime_5) {
                            LogToFile.d("====-交易明细-====", "无转账记录判定超时");
                            notList = false;
                            break;
                        }
                        UiObject mTV_notList = mDevice.findObject(new UiSelector().className("android.view.View").text
                                ("该时间段内无查询记录"));
                        if (mTV_notList.exists()) {
                            LogToFile.d("====-交易明细-====", "无转账记录");
                            notList = true;
                            sleep(2000);
                            break;
                        }
                    }
                    s5TransactionDetail(notList);
                    //删除处理过的订单
                    mapDelete(list.get(0).getOrder());
                    getmap();
                    backMainPage();
                    sleep(ChatClient.overtime_3);
                    clickListPage();
                    sleep(2000);
                    return;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Samsung(1920)交易明细
     */
    private void samsungTransactionDetail(boolean notList) throws UiObjectNotFoundException {
        showMsg("进入三星手机交易明细（）");
        String balance = "-1";
        String bankNo_ = "";
        String name = "";
        String status = "";
        String json;
        String mobileD;
        if (!notList) {
            UiObject scrollWrap = mDevice.findObject(new UiSelector().resourceId("scrollWrap").className("android" +
                    ".view.View"));
            showMsg("scrollWrap");
            UiObject child = scrollWrap.getChild(new UiSelector().className("android.view.View").index(0));
            showMsg("child");
            for (int i = 0; i < 5; i++) {//往下查5条
                UiObject mLV = child.getChild(new UiSelector().className("android.view.View").index(4 + i));
                showMsg("mLV");
                if (mLV.exists()) {
                    UiObject mItem = mLV.getChild(new UiSelector().className("android.widget.ListView").index(1));
                    if (mItem.exists()) {
                        showMsg("找到条目位置" + (4 + i));
                        UiObject child2 = mItem.getChild(new UiSelector().className("android.view.View").index(0));
                        UiObject mTV_nameOrBnakNoOrDate = mItem.getChild(new UiSelector().className
                                ("android.view.View").index(1));
                        UiObject mTV_status = mItem.getChild(new UiSelector().className("android.view.View")
                                .instance(5));
                        if (child2.exists()) {
                            balance = child2.getText();
                            if (StringUtils.isEmpty(balance)) {
                                balance = child2.getContentDescription();
                            }
                            if (balance.contains("转账汇款")) {
                                showMsg("为转账汇款订单");
                                if (mTV_nameOrBnakNoOrDate.exists()) {
                                    UiObject mTV_nameOrBnakNo = mTV_nameOrBnakNoOrDate.getChild(new UiSelector()
                                            .className("android.view.View").index(0));
                                    if (mTV_nameOrBnakNo.exists()) {
                                        String nameOrBnakNo = mTV_nameOrBnakNo.getText();
                                        if (StringUtils.isEmpty(nameOrBnakNo)) {
                                            nameOrBnakNo = mTV_nameOrBnakNo.getContentDescription();
                                        }
                                        if (!StringUtils.isEmpty(nameOrBnakNo)) {
                                            String[] strings = nameOrBnakNo.split("\\(");
                                            name = strings[0];
                                            bankNo_ = nameOrBnakNo.substring(nameOrBnakNo.length() - 5, nameOrBnakNo
                                                    .length() - 1);
                                            showMsg("name===" + name + "---bankNo_===" + bankNo_);
                                            if (mTV_status.exists()) {
                                                String s = mTV_status.getText();
                                                if (StringUtils.isEmpty(s)) {
                                                    s = mTV_status.getContentDescription();
                                                }
                                                if (!StringUtils.isEmpty(s)) {
                                                    if (s.contains("交易失败")) {
                                                        status = "交易失败";
                                                    } else {
                                                        status = s;
                                                    }
                                                    showMsg("status===" + status + "---s===" + s);
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                sleep(1000);
            }
            balance = balance.trim().replaceAll("转账汇款", "");
            balance = balance.replaceAll("元", "");
            balance = balance.replaceAll(",", "");
        }
        String bankNo0 = list.get(0).getBankNo();
        bankNo0 = bankNo0.substring(bankNo0.length() - 4);
        showMsg("balance===" + balance + "===name===" + name + "===bankNo_===" + bankNo_ + "===status===" + status);
        if (!StringUtils.isEmpty(bankNo_) && !StringUtils.isEmpty(name) && Double.parseDouble(list.get(0).getMoney())
                == Double.parseDouble(balance) && bankNo_.equals(bankNo0) && name.equals(list.get(0).getUserName())
                && !status.equals("交易失败")) {
            mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "'," +
                    "'phoneModel':'光大银行'," + "'info':'" + status + "'";
            if (status.contains("交易成功")) {//确定状态成功
                json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':0,'type':1";
            } else {//状态待确认
                json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':6,'type':1";
            }
        } else {//状态失败
            json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':4,'type':1";
            mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "'," +
                    "'phoneModel':'光大银行'," + "'info':'交易失败'";
        }
        ChatClient.msg("<YZF>{'header':'" + ChatClient.S_CONFIRM_TRADE_RESULT_C + "'," + json + "," + mobileD +
                "}</YZF>");
    }

    /**
     * S5(2016)交易明细
     */
    private void s5TransactionDetail(boolean notList) throws UiObjectNotFoundException {
        String balance = "-1";
        String bankNo_ = "";
        String name = "";
        String status = "";
        String json = null;
        String mobileD = null;
        if (!notList) {
            UiObject scrollWrap = mDevice.findObject(new UiSelector().resourceId("scrollWrap").className("android" +
                    ".view.View"));
            UiObject child = scrollWrap.getChild(new UiSelector().className("android.view.View").index(0));
            UiObject mLV = child.getChild(new UiSelector().className("android.view.View").index(3));
            for (int i = 0; i < 5; i++) {//往下查5条
                UiObject child1 = mLV.getChild(new UiSelector().className("android.view.View").index(i));
                if (child1.exists()) {
                    UiObject mItem = child1.getChild(new UiSelector().className("android.widget.ListView").index(1));
                    if (mItem.exists()) {
                        showMsg("找到条目位置" + i);
                        UiObject child2 = mItem.getChild(new UiSelector().className("android.view.View").index(0));
                        UiObject mTV_nameOrBnakNoOrDate = mItem.getChild(new UiSelector().className
                                ("android.view.View").index(1));
                        UiObject mTV_status = mItem.getChild(new UiSelector().className("android.view.View")
                                .instance(5));
                        if (child2.exists()) {
                            balance = child2.getText();
                            if (StringUtils.isEmpty(balance)) {
                                balance = child2.getContentDescription();
                            }
                            if (balance.contains("转账汇款")) {
                                showMsg("为转账汇款订单");
                                if (mTV_nameOrBnakNoOrDate.exists()) {
                                    UiObject mTV_nameOrBnakNo = mTV_nameOrBnakNoOrDate.getChild(new UiSelector()
                                            .className("android.view.View").index(0));
                                    if (mTV_nameOrBnakNo.exists()) {
                                        String nameOrBnakNo = mTV_nameOrBnakNo.getText();
                                        if (StringUtils.isEmpty(nameOrBnakNo)) {
                                            nameOrBnakNo = mTV_nameOrBnakNo.getContentDescription();
                                        }
                                        if (!StringUtils.isEmpty(nameOrBnakNo)) {
                                            String[] strings = nameOrBnakNo.split("\\(");
                                            name = strings[0];
                                            bankNo_ = nameOrBnakNo.substring(nameOrBnakNo.length() - 5, nameOrBnakNo
                                                    .length() - 1);
                                            showMsg("name===" + name + "---bankNo_===" + bankNo_);
                                            if (mTV_status.exists()) {
                                                String s = mTV_status.getText();
                                                if (StringUtils.isEmpty(s)) {
                                                    s = mTV_status.getContentDescription();
                                                }
                                                if (!StringUtils.isEmpty(s)) {
                                                    if (s.contains("交易失败")) {
                                                        status = "交易失败";
                                                    } else {
                                                        status = s;
                                                    }
                                                    showMsg("status===" + status + "---s===" + s);
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                sleep(1000);
            }
            balance = balance.trim().replaceAll("转账汇款", "");
            balance = balance.replaceAll("元", "");
            balance = balance.replaceAll(",", "");
        }
        String bankNo0 = list.get(0).getBankNo();
        bankNo0 = bankNo0.substring(bankNo0.length() - 4);
        showMsg("balance===" + balance + "===name===" + name + "===bankNo_===" + bankNo_ + "===status===" + status);
        if (!StringUtils.isEmpty(bankNo_) && !StringUtils.isEmpty(name) && Double.parseDouble(list.get(0).getMoney())
                == Double.parseDouble(balance) && bankNo_.equals(bankNo0) && name.equals(list.get(0).getUserName())
                && !status.equals("交易失败")) {
            mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "'," +
                    "'phoneModel':'光大银行'," + "'info':'" + status + "'";
            if (status.contains("交易成功")) {//确定状态成功
                json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':0,'type':1";
            } else {//状态待确认
                json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':6,'type':1";
            }
        } else {//状态失败
            String sumMoneyFile = user.getUserName() + bankName + "_sumMoney.txt";
            String oldMoney = FileHelper.readSDFile(sumMoneyFile);
            switch (Constants.changeInAmount(bankName, oldMoney, sumMoney, list.get(0).getMoney())) {
                case "0":
                    json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':0,'type':1";
                    mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "','phoneModel':'光大银行'," +
                            "'info':'交易成功'";
                    break;
                case "1":
                    json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':1,'type':1";
                    mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "','phoneModel':'光大银行'," +
                            "'info':'交易待处理...'";
                    break;
                case "4":
                    json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':4,'type':1";
                    mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "','phoneModel':'光大银行'," +
                            "'info':'未查到条目'";
                    break;
            }
        }
        ChatClient.msg("<YZF>{'header':'" + ChatClient.S_CONFIRM_TRADE_RESULT_C + "'," + json + "," + mobileD + "}</YZF>");
    }

    /**
     * 转账订单状态上报
     *
     * @param info  状态
     * @param state 成功与否
     */
    private void reportTransferOrderStatus(String info, int state) {
        showMsg("转账状态---" + info);
        String mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "','phoneModel':'光大银行'," +
                "'info':'" + info + "'";
        String json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':" + state + "," +
                "'type':1";
        showMsg("mobileD = " + mobileD);
        showMsg("json = " + json);
        ChatClient.msg("<YZF>{'header':'" + ChatClient.M_SEND_TRADE_RESULT_P + "'," + json + "," + mobileD + "}</YZF>");
        mapDelete(list.get(0).getOrder());
        getmap();
    }

    /**
     * 等待订单境内汇款页面
     */
    public void remittance1() {
        try {
            clickListPage();
            ChatClient.request();
            //进入转账页面
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime_60) {
                    showMsg("查找余额超时");
                    backMainPage();
                    return;
                }
                UiObject money1 = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                        ".cemb:id/tv_transfer_input_account_of_payment_balance"));
                if (money1.exists()) {
                    showMsg("可用余额:" + money1.getText());
                    sumMoney = money1.getText();
                    sumMoney = sumMoney.replaceAll(",", "");
                    showMsg("===moeny1====" + sumMoney + "===");
                    if (!StringUtils.isEmpty(sumMoney)) {
                        C_updateBalance_W(Double.parseDouble(sumMoney));
                        showMsg("====上报余额完毕====");
                        showMsg("本地保存余额");
                        String sumMoneyFile = user.getUserName() + bankName + "_sumMoney.txt";
                        FileHelper.saveMoneySDFile(false, sumMoney, sumMoneyFile);
                        sleep(ChatClient.overtime_10);
                        break;
                    }
                }
            }

            backMainPage();
        } catch (Exception e) {
            setDevicesLog(e.getCause().getMessage());
            ChatClient.timer("remittance1", ChatClient.error);
            e.printStackTrace();
        }
    }

    /**
     * 判定是否有错误弹窗
     *
     * @return 错误状态
     * @throws UiObjectNotFoundException 异常捕获
     */
    private String checkSmsByCommitError() throws UiObjectNotFoundException {
        UiObject errorBtn;
        ChatClient.begintime = System.currentTimeMillis();
        while (true) {
            long nowTime = System.currentTimeMillis();
            if (nowTime - ChatClient.begintime > ChatClient.overtime_8) {
                LogToFile.d("====动态口令====", "===动态口令检测完成====");
                return SUCCESS;
            }
            String errorStr = "";
            UiObject errorMsg = mDevice.findObject(new UiSelector().
                    resourceId("com.cebbank.mobile.cemb:id/dialog_tv_msg"));
            errorBtn = mDevice.findObject(new UiSelector().
                    resourceId("com.cebbank.mobile.cemb:id/dialog_btn_positive"));

            if (errorMsg.exists()) {
                if (!TextUtils.isEmpty(errorMsg.getText())) {
                    errorStr = errorMsg.getText();
                } else if (!TextUtils.isEmpty(errorMsg.getContentDescription())) {
                    errorStr = errorMsg.getContentDescription();
                }
                if (errorStr.contains("无效的动态口令") || errorStr.contains("动态口令不符合格式要求,请重新输入")) {
                    showMsg("===无效的动态口令====");
                    if (errorBtn.exists()) {
                        errorBtn.click();
                    }
                    sleep(2000);
                    UiObject message = mDevice.findObject(new UiSelector().
                            resourceId("com.cebbank.mobile.cemb:id/et_transfer_payment_view_input_identifying_code"));

                    if (message.exists()) {
                        int length = message.getText().length();
                        message.click();
                        sleep(1000);
                        showMsg("删除错误验证码");
                        for (int i = 0; i < length; i++) {
                            mDevice.click(720, 1170);
                            sleep(1000);
                        }
                        showMsg("收起键盘");
                        mDevice.click(400, 700);
                    }
                    return SMS_ERR;
                } else {
                    if (errorBtn.exists()) {
                        errorBtn.click();
                        return errorStr;
                    }
                }
            }
        }
    }

    /**
     * 返回
     */
    public static void back() {
        //进入转账页面
        mDevice.pressBack();
    }


    /**
     * 返回主页
     */
    public void backMainPage() {
        //进入转账页面
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtime_8) {
                showMsg("===返回上页===");
                mDevice.pressBack();
                ChatClient.begintime = System.currentTimeMillis();//开始时间
            }
            UiObject mTV_transfer = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                    ".cemb:id/tv_transfer_main_transfer").text("转账汇款"));
            if (mTV_transfer.exists()) {
                showMsg("===已返回转账主页===");
                break;
            }
        }
    }

    /**
     * 进入账单页面
     */
    public void clickListPage() throws UiObjectNotFoundException {
        //进入转账页面
        while (true) {
            UiObject main = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                    ".cemb:id/ll_imqq_transfer_remittance"));
            if (main.exists()) {
                showMsg("===点击主页转账汇款===");
                main.click();
                sleep(ChatClient.overtime_5);
            }
            UiObject mTV_transfer = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                    ".cemb:id/tv_transfer_main_transfer").text("转账汇款"));
            if (mTV_transfer.exists()) {
                showMsg("===点击转账页转账汇款2===");
                mTV_transfer.click();
                sleep(ChatClient.overtime_5);
            }
            UiObject transferList = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                    ".cemb:id/et_transfer_input_first_transfer_payee_name").text("请输入收款户名/首字母检索"));
            if (transferList.exists()) {
                showMsg("===以到转账单页面===");
                break;
            }
        }
    }

    /**
     * 模拟点击登录密码
     *
     * @param pad_ 密码
     */
    public void setClick(String pad_) {
        char[] abc = "1234567890qwertyuiopasdfghjklzxcvbnm".toCharArray();//char数组
        char[] pwd = pad_.toCharArray();//char数组
        for (int j = 0; j < pwd.length; j++) {
            for (int i = 0; i < abc.length; i++) {
                if (abc[i] == pwd[j]) {
                    mDevice.click(xy_123_abc[i][0], xy_123_abc[i][1]);
                    sleep(1000);
                    break;
                }
            }
        }
    }

    /**
     * 模拟点击密码
     *
     * @param pad_ 异常捕获
     */
    public static void setClick_bankPwd(String pad_) {
        char[] abc = "1234567890".toCharArray();//char数组
        char[] pwd = pad_.toCharArray();//char数组
        for (int j = 0; j < pwd.length; j++) {
            for (int i = 0; i < abc.length; i++) {
                if (abc[i] == pwd[j]) {
                    UiDevice.getInstance().click(xy_123[i][0], xy_123[i][1]);
                    sleep(1000);
                    break;
                }
            }
        }
    }


    /**
     * 模拟点击短信验证码
     *
     * @param pad_ 异常捕获
     */
    public void setClick_SMS(String pad_) throws UiObjectNotFoundException {
        char[] abc = "123456789.0".toCharArray();//char数组
        char[] pwd = pad_.toCharArray();//char数组
        for (int j = 0; j < pwd.length; j++) {
            for (int i = 0; i < abc.length; i++) {
                if (abc[i] == pwd[j]) {
                    UiDevice.getInstance().click(sms_123[i][0], sms_123[i][1]);
                    sleep(1000);
                    break;
                }
            }
        }
        // 收起键盘
        while (true) {
            UiObject mBT = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile.cemb:id/kb_confirm"));
            if (mBT.exists()) {
                showMsg("====短信验证码输入完成，点击键盘确定收回键盘====");
                mBT.click();
                break;
            }
        }
    }

    private void checkSms() throws UiObjectNotFoundException {
        mDevice.pressBack();
        while (true) {
            sleep(1000);
            UiObject ui = mDevice.findObject(new UiSelector().resourceId("com.cebbank.mobile" +
                    ".cemb:id/et_transfer_payment_view_input_identifying_code"));
            if (ui.exists()) {
                String str = ui.getText();
                if (!TextUtils.isEmpty(str) && str.length() == 6) {
                    break;
                }
            }
        }
    }

    public void showMsg(String msg) {
        LogToFile.showLog("光大银行", msg);
    }
}
